<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderno">
        <el-input
          v-model="queryParams.orderno"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入仓日期" prop="warehousingTime">
        <el-date-picker
          @keyup.enter.native="handleQuery"
          value-format="yyyy-MM-dd"
          v-model="queryParams.warehousingTime"
          type="date"
          placeholder="请选择入仓日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="存放区号" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入存放区号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户代码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档口地址" prop="sellerAddress">
        <el-input
          v-model="queryParams.sellerAddress"
          placeholder="请输入档口地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择打单状态">
          <el-option label="全部" value="0"></el-option>
          <el-option label="未打单" value="1"></el-option>
          <el-option label="已打单" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属国家" prop="country">
        <el-select v-model="queryParams.country" placeholder="请选择订单所属国家">
          <el-option label="全部" :value="-1"></el-option>
          <el-option label="其他国家" :value="0"></el-option>
          <el-option :label="item.text" :value="item.code" v-for="(item, index) in countryOptions"
                     :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-printer"
          size="mini"
          @click="handlePrint"
          v-hasPermi="['system:order:add']"
        >批量打单
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-box"
          size="mini"
          @click="handleCounter"
          v-hasPermi="['system:order:export']"
        >装柜
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-row :gutter="20">
            <el-col :span="6" v-for="(item, index) in rowInfo" :key="index">
              <div class="table-info-item">
                <span>{{ item.label }}</span>
                <span v-if="item.name == 'status'">
                  <el-tag effect="dark" :type="props.row[item.name] == 1 ? 'info' : ''">{{
                      props.row[item.name] == 1 ? '未打单' : '已打单'
                    }}</el-tag>
                </span>
                <span v-if="item.name != 'status'">{{ props.row[item.name] }}</span>
              </div>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" prop="orderno" width="150"/>
      <el-table-column label="客户名称" prop="customerName" min-width="120"/>
      <el-table-column label="客户电话" prop="customerPhone" min-width="120"/>
      <el-table-column label="客户国家" prop="customerCountry" min-width="120"/>
      <el-table-column label="货物名称" prop="goodsName" min-width="120"/>
      <el-table-column label="存放区号" prop="area" max-width="120" align="center"/>
      <el-table-column label="包数量" prop="packageNum" align="center" max-wdith="100"/>
      <el-table-column label="总件数" prop="total" align="center" max-wdith="100"/>
      <el-table-column label="货物体积" prop="goodsSize" align="center" max-wdith="100"/>
      <el-table-column label="是否已打单" align="center" prop="status" width="120">
        <template slot-scope="props">
          <el-tag :type="props.row.status == 1 ? 'info' : ''">{{
              props.row.status == 1 ? '未打单' : '已打单'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column property="printAccount" align="center" label="开单账户" min-width="100"></el-table-column>
      <el-table-column property="printName" align="center" label="开单人" min-width="100"></el-table-column>
      <el-table-column label="入仓日期" prop="warehousingTime" width="140"/>
      <el-table-column label="操作" align="center" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="handlePrint(scope.row)"
            v-hasPermi="['system:order:remove']"
          >打印
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="货柜列表" :visible.sync="counterTableVisible" :fullscreen="counterInfo.fullscreen" width="850px">
      <el-form :model="counterInfo.queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="货柜编码" prop="code">
          <el-input
            v-model="counterInfo.queryParams.code"
            placeholder="请输入货柜编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="货柜主人" prop="master">
          <el-input
            v-model="counterInfo.queryParams.master"
            placeholder="请输入货柜主人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="counterInfo.list">
        <el-table-column property="code" label="货柜编码" min-width="200"></el-table-column>
        <el-table-column property="lockCode" label="锁头编码" min-width="200"></el-table-column>
        <el-table-column property="master" label="货柜主人" min-width="120"></el-table-column>
        <el-table-column property="masterPhone" label="货柜负责人电话" min-width="200"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-box"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:order:remove']"
            >装柜
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="counterInfo.total > 0"
        :total="counterInfo.total"
        :page.sync="counterInfo.queryParams.pageNum"
        :limit.sync="counterInfo.queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>

    <el-dialog
      title="打印单据"
      :visible.sync="printerDialog"
      :open="openPrinterDialog"
      :close="cancelPrint"
      width="500px">
      <el-form :model="printerForm"
               :rules="printerFormRules"
               ref="printerFormRef"
               label-position="top"
               label-width="68px">
        <el-form-item label="打印机" prop="printerDeviceId">
          <el-select v-model="printerForm.printerDeviceId"
                     placeholder="请选择打印机"
                     prop="printerDeviceId"
                     style="width: 100%;">
            <el-option v-for="(item, index) in printerDeviceList"
                       :label="item.label"
                       :value="item.value"
                       :key="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelPrint" :loading="printerLoading">取 消</el-button>
        <el-button type="primary" @click="submitPrint" :loading="printerLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, delOrder, printOrder} from "@/api/system/order";
import {listCounter} from "@/api/system/counter";
import {getCountryListApiV2, getPrinterListApi} from "@/api/system/common";

export default {
  name: "Order",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      counterInfo: {
        fullscreen: false,
        width: '',
        list: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,

        },
        total: 10
      },
      counterTableVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderno: null,
        companyId: null,
        warehousingTime: null,
        area: null,
        packageNum: null,
        total: null,
        customerName: null,
        customerCode: null,
        sellerAddress: null,
        sellerPhone: null,
        goodsName: null,
        status: null,
        country: -1
      },
      // 表单参数
      form: {},
      // 表格展开详情字段
      rowInfo: [
        {name: 'orderno', label: '订单编号：'},
        {name: 'companyName', label: '公司名称：'},
        {name: 'warehousingTime', label: '入仓日期：'},
        {name: 'area', label: '存放区号：'},
        {name: 'packageNum', label: '包数量：'},
        {name: 'total', label: '总数量：'},
        {name: 'customerName', label: '客户姓名：'},
        {name: 'customerCode', label: '客户代码：'},
        {name: 'sellerAddress', label: '档口地址：'},
        {name: 'sellerPhone', label: '档口电话：'},
        {name: 'goodsName', label: '货物名称：'},
        {name: 'goodsSize', label: '货物体积：'},
        {name: 'status', label: '打单状态：'},
        {name: 'createBy', label: '订单创建人：'},
        {name: 'createTime', label: '订单创建日期：'},
      ],
      // 表单校验
      rules: {
        orderno: [
          {required: true, message: "订单编号不能为空", trigger: "blur"}
        ],
        companyId: [
          {required: true, message: "订单所属公司不能为空", trigger: "blur"}
        ],
        warehousingTime: [
          {required: true, message: "入仓日期不能为空", trigger: "blur"}
        ],
        area: [
          {required: true, message: "存放区号不能为空", trigger: "blur"}
        ],
        customerName: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ],
        sellerAddress: [
          {required: true, message: "档口地址不能为空", trigger: "blur"}
        ],
        sellerPhone: [
          {required: true, message: "档口电话不能为空", trigger: "blur"}
        ],
        goodsName: [
          {required: true, message: "货物名称不能为空", trigger: "blur"}
        ],
      },
      countryOptions: [],
      printerDialog: false,
      printerLoading: false,
      printerDeviceList: null,
      printerForm: {
        ids: [],
        printerDeviceId: null,
      },
      printerFormRules: {
        printerDeviceId: [
          {required: true, message: "请选择打印机", trigger: "change"}
        ],
      }
    };
  },
  created() {
    if (document.documentElement.clientWidth < 800) {
      this.counterInfo.fullscreen = true;
    }
    this.getCountryList()
    this.getList();
  },
  methods: {
    getCountryList() {
      getCountryListApiV2().then((res) => {
        this.countryOptions = res.data
      })
    },
    /** 查询客户订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/warehouse/order-save/index");
    },
    handlePrint(row) {
      const ids = row.id ? [row.id] : this.ids;
      if(!ids || ids.length === 0) {
        this.$message.warning("至少选择一条数据");
        return;
      }
      this.printerForm.ids = ids
      this.openPrinterDialog()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push(`/warehouse/order-update/index/${row.id}`);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /** 装柜 */
    async handleCounter() {
      // if (this.ids.length == 0) {
      //   this.$message.warning("最少选择一条订单");
      //   return;
      // }
      const data = await listCounter(this.counterInfo.queryParams);
      this.counterInfo.list = data.rows;
      this.counterInfo.total = data.total;
      this.counterTableVisible = true;
    },
    openPrinterDialog() {
      this.printerDialog = true;
      this.printerLoading = true;
      getPrinterListApi().then((res) => {
        this.printerDeviceList = res.data
      }).finally(() => {
        this.printerLoading = false;
      })
    },
    submitPrint() {
      this.$refs["printerFormRef"].validate((valid) => {
        if (valid) {
          this.printerLoading = true;
          printOrder(this.printerForm).then(() => {
            this.$message.success("操作成功")
          }).finally(() => {
            this.printerLoading = false;
          })
        }
      })
    },
    cancelPrint() {
      this.printerDialog = false;
      this.printerForm = {
        ids: [],
        printerDeviceId: null,
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.table-info-item {
  height: 40px;
  display: flex;
  align-items: center;
  font-size: 15px;
}

::v-deep .el-dialog__body {
  padding: 10px 10px !important;
}
</style>
